package com.zyzg.service;

import com.zyzg.domain.AppConfig;
import com.zyzg.mapper.DataMapper;
import com.zyzg.utils.AESUtil;
import com.zyzg.utils.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppConfigSyncService implements CommandLineRunner {
    @Autowired
    private DataMapper dataMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        syncDataFromDbToRedis();
    }

    public void syncDataFromDbToRedis() {
        List<AppConfig> appConfigs = dataMapper.findAllAppConfig();
        for (AppConfig appConfig : appConfigs) {
            try {
                redisCache.setCacheObject(appConfig.getAppId(), AESUtil.encrypt(appConfig.getAppSecret(), appConfig.getSecretKey()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}